package bishi;

import java.util.*;

public class Code360_2 {

    public static void main(String args[]) throws Exception {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long ans = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            if (b == 0) {
                ans += a;
            } else {
                list.add(a);
            }
        }
        Integer[] arr = list.toArray(new Integer[list.size()]);
        Arrays.sort(arr, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for (int i = 0; i < arr.length; i++) {
            ans = Math.max((arr[i] + ans), ans * 2);
        }
        System.out.println(ans);



    }

}